import { createRouter, createWebHistory } from 'vue-router';
import adminIndex from '@/views/admin/adminIndex/index.vue';
import Dept from '@/views/admin/dept/index.vue';
import Emp from '@/views/admin/emp/index.vue';
import EmpReport from '@/views/admin/report/emp/index.vue';

import Login from '@/views/login/index.vue';
import adminLayout from '@/views/admin/adminLayout/index.vue';
import UserLayout from '@/views/user/userLayout/index.vue';
import EmployeeLayout from '@/views/employee/empLayout/index.vue';
import User from '@/views/admin/user/index.vue'
import Car from '@/views/admin/car/index.vue'
import Job from '@/views/admin/job/index.vue'
import UserIndex from '@/views/user/userIndex/index.vue'

import { ElMessage } from 'element-plus'
const routes = [
  { 
    path: '/admin', 
    component: adminLayout,
    redirect: '/adminIndex/index',
    children: [
      { path: '/index', component: adminIndex },
      { path: '/dept', component: Dept },
      { path: '/emp', component: Emp },
      { path: '/report/emp', component: EmpReport },

      { path: '/user', component: User },
      { path: '/car', component: Car },
      { path: '/job', component: Job },
      { path: '/maintenance/index', component: () => import('@/views/admin/maintenance/index.vue') },
      { path: '/repair/index', component: () => import('@/views/admin/repair/index.vue') },
      { path: '/feedback', component: () => import('@/views/admin/feedback/index.vue') }
    ]
  },
  //用户端路由
  {
    path: '/user',
    component: UserLayout,
    redirect: '/userIndex/index',
    children: [
      { path: 'index', component: UserIndex },
      // 其他用户页面路由将在这里添加
      { path: 'mycars', component: () => import('@/views/user/mycars/index.vue') },
      { path: 'appointment', component: () => import('@/views/user/appointment/index.vue') },
      { path: 'records', component: () => import('@/views/user/records/index.vue') },
      { path: 'feedback', component: () => import('@/views/user/myFeedback/index.vue') },
      { path: 'personalCenter', component: () => import('@/views/user/personalCenter/index.vue') }
    ]
  },
  // 员工端路由
  {
    path: '/employee',
    component: EmployeeLayout,
    redirect: '/employee/index',
    children: [
      { path: 'index', component: () => import('@/views/employee/empIndex/index.vue') },
      { path: 'maintenance', component: () => import('@/views/employee/myMaintenance/index.vue') },
      { path: 'repair', component: () => import('@/views/employee/myRepair/index.vue') },
      { path: 'customer', component: () => import('@/views/employee/customer/index.vue') },
      
      { path: 'cars', component: () => import('@/views/employee/cars/index.vue') }
    ]
  },
  { path: '/login', component: Login }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  // 不需要登录的页面直接放行
  if (to.path === '/login') {
    next();
    return;
  }
  
  // 获取登录信息
  const loginUser = JSON.parse(localStorage.getItem('loginUser'));
  
  // 未登录，跳转到登录页
  if (!loginUser) {
    ElMessage.error('请先登录');
    next('/login');
    return;
  }
  
  // 优化路由守卫逻辑，解决管理员登录跳转问题
  // 处理根路径重定向
    if (to.path === '/') {
      if (loginUser.role === 'admin') {
        // 管理员登录后跳转到后台首页
        next('/index');
      } else if (loginUser.role === 'employee') {
        // 员工登录后跳转到员工仪表盘
        next('/employee/index');
      } else {
        // 普通用户登录后跳转到用户主页
        next('/user/index');
      }
      return;
    }
  
  // 区分后台管理页面、员工页面和用户页面
  if (to.path.startsWith('/user/')) {
    // 用户页面，所有已登录用户都可以访问
    next();
  } else if (to.path.startsWith('/employee/')) {
    // 员工页面，只有员工可以访问
    if (loginUser.role === 'employee') {
      next();
    } else {
      ElMessage.error('权限不足，无法访问员工页面');
      next(from.path || '/');
    }
  } else {
    // 后台管理页面，只有管理员可以访问
    if (loginUser.role === 'admin') {
      next();
    } else {
      // 员工跳转到员工首页，其他用户跳转到用户首页
      next(loginUser.role === 'employee' ? '/employee/index' : '/user/index');
    }
  }
});

export default router;